import { BaseResponse, AjaxRequest } from './index'
import {
  MediaId,
  MediaInfo,
  MediaRemoveReqParam,
  MediaSearchReqParam,
  MediaUploadReqParam,
  MediauUpdateTitleReqParam,
  ResPageList,
} from '@/core/mediaRecorder/apiType'
import { CancelToken } from 'axios'
const fetch = new AjaxRequest({
  baseURL: import.meta.env.VITE_API_HAICHUAN_VIDEO_DOMAIN,
})

/**
 * 更新信息
 * @param {string} token 集团登录返回的token
 * @returns {object} 用户信息
 */
export async function updateInfo(param: MediauUpdateTitleReqParam): Promise<BaseResponse<null>> {
  const { data } = await fetch.post<BaseResponse<null>>('/api/media/updateInfo', { ...param })
  return data
}

/**
 * 删除media
 * @returns {object} 状态信息
 */
export async function deleteMedia(param: MediaRemoveReqParam): Promise<BaseResponse<null>> {
  const { data } = await fetch.post<BaseResponse<null>>('/api/media/delete', { ...param })
  return data
}

/**
 * 删除media
 * @returns {object} 状态信息
 */
export async function synthesisRetryMedia(
  param: Pick<MediaInfo, 'mediaId' | 'ctimeFormat'>,
): Promise<BaseResponse<null>> {
  const { data } = await fetch.get<BaseResponse<null>>('/api/media/synthesisRetry', { ...param })
  return data
}

/**
 * 上传
 * @returns {object} 状态信息
 */
export async function uploadMedia(
  param: MediaUploadReqParam,
  progress: (event: ProgressEvent) => void,
  sourceToken: CancelToken,
): Promise<BaseResponse<{ mediaId: MediaId }>> {
  const { data } = await fetch._ajax({
    url: '/api/media/upload',
    method: 'post',
    timeout: 0,
    data: param,
    onUploadProgress: progress,
    cancelToken: sourceToken,
    headers: { 'Content-Type': 'multipart/form-data' },
  })
  return data
}

/**
 *
 * @returns {object} 状态信息
 */
export async function getMediaList(
  param: MediaSearchReqParam,
): Promise<BaseResponse<ResPageList<MediaInfo>>> {
  const { data } = await fetch.get<BaseResponse<ResPageList<MediaInfo>>>('/api/media/getList', {
    ...param,
  })
  return data
}
